package com.wlz.file;

import cn.hutool.core.util.ZipUtil;
import com.wlz.util.Zip4jUtil;
import com.wlz.util.ZipUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * @author wlz
 * @date 2022-11-01  10:28 上午
 */
@Slf4j
public class FileZipTest {

    public static void main(String[] args) throws IOException {
        long time = System.currentTimeMillis();
        String fullPath = "/Users/wlz/Desktop/20231030/ICCE00141245S001011N50ABALL/ICCE00141245S001011N50ABALL.zip";
//        hutoolZip(fullPath);
        zip4j(fullPath);
        String unPackageOriginalPath = fullPath.substring(0, fullPath.lastIndexOf("."));
        File unZipFile = new File(unPackageOriginalPath);
        List<File> unZipFileList = Arrays.asList(unZipFile.listFiles());
        System.out.println(unZipFileList);
//        javaZip(fullPath);
//        zip(fullPath);
        System.out.println("time : " + (System.currentTimeMillis() - time));
    }

    public static void zip(String fullPath) {
        String unPackageOriginalPath = fullPath.substring(0, fullPath.lastIndexOf("."));
        ZipUtils.unZip(fullPath, unPackageOriginalPath);
    }

    public static void hutoolZip(String fullPath) {
        String unPackageOriginalPath = fullPath.substring(0, fullPath.lastIndexOf("."));
        File unzip = ZipUtil.unzip(fullPath, unPackageOriginalPath, Charset.forName("GBK"));
    }


    public static void javaZip(String fullPath) throws IOException {
        File file = new File(fullPath);
        String unPackageOriginalPath = fullPath.substring(0, fullPath.lastIndexOf("."));
        unZipFiles(file, unPackageOriginalPath);
    }

    public static void zip4j(String fullPath) {
        String unPackageOriginalPath = fullPath.substring(0, fullPath.lastIndexOf("."));
        Zip4jUtil.zip4j(fullPath, unPackageOriginalPath);
    }


    /**
     * 解压文件
     *
     * @param zipFile：需要解压缩的文件
     * @param descDir：解压后的目标目录
     */
    public static void unZipFiles(File zipFile, String descDir) throws IOException {
        File destFile = new File(descDir);
        if (!destFile.exists()) {
            destFile.mkdirs();
        }
        // 解决zip文件中有中文目录或者中文文件
        ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));
        for (Enumeration entries = zip.entries(); entries.hasMoreElements(); ) {
            ZipEntry entry = (ZipEntry) entries.nextElement();
            InputStream in = zip.getInputStream(entry);
            String curEntryName = entry.getName();
            // 判断文件名路径是否存在文件夹
            int endIndex = curEntryName.lastIndexOf('/');
            // 替换
            String outPath = (descDir + curEntryName).replaceAll("\\*", "/");
            if (endIndex != -1) {
                File file = new File(outPath.substring(0, outPath.lastIndexOf("/")));
                if (!file.exists()) {
                    file.mkdirs();
                }
            }

            // 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
            File outFile = new File(outPath);
            if (outFile.isDirectory()) {
                continue;
            }
            OutputStream out = new FileOutputStream(outPath);
            byte[] buf1 = new byte[1024];
            int len;
            while ((len = in.read(buf1)) > 0) {
                out.write(buf1, 0, len);
            }
            in.close();
            out.close();
        }
        System.out.println("解压 {} 成功"+  zipFile.getName());
    }
}
